#include <iostream>
#include <iterator>
#include <vector>
#include <list>
#include <set>
#include <algorithm>

using std::cout;
using std::endl;
using std::vector;
using std::list;
using std::set;
using std::ostream_iterator;
using std::back_inserter;
using std::back_insert_iterator;
using std::front_inserter;
using std::front_insert_iterator;
using std::inserter;
using std::insert_iterator;

void test()
{
    vector<int> vecNum = {1, 3, 5, 7};
    copy(vecNum.begin(), vecNum.end(), 
         ostream_iterator<int>(cout, "  "));
    cout << endl;

    list<int> listNum = {2, 4, 6, 8};
    //back_inserter函数会返回back_insert_iterator，该类的赋值
    //函数的底层会调用push_back
    /* copy(listNum.begin(), listNum.end(), back_inserter(vecNum)); */
    copy(listNum.begin(), listNum.end(), 
         back_insert_iterator<vector<int>>(vecNum));
    copy(vecNum.begin(), vecNum.end(), 
         ostream_iterator<int>(cout, "  "));
    cout << endl;

    cout << endl << endl;
    //front_inserter函数会返回front_insert_iterator，该类的赋值
    //函数的底层会调用push_front
    /* copy(vecNum.begin(), vecNum.end(), front_inserter(listNum)); */
    copy(vecNum.begin(), vecNum.end(), 
         front_insert_iterator<list<int>>(listNum));
    copy(listNum.begin(), listNum.end(), 
         ostream_iterator<int>(cout, "  "));
    cout << endl;

    cout << endl << endl;
    set<int> setNum = {1, 3, 5, 9, 12, 34, 7, 0};
    auto it = setNum.end();
    --it;
    //inserter函数会返回insert_iterator，该类的赋值
    //函数的底层会调用insert
    /* copy(vecNum.begin(), vecNum.end(), inserter(setNum, it)); */
    copy(vecNum.begin(), vecNum.end(), 
         insert_iterator<set<int>>(setNum, it));
    copy(setNum.begin(), setNum.end(), 
         ostream_iterator<int>(cout, "  "));
    cout << endl;
}

int main(int argc, char *argv[])
{
    test();
    return 0;
}

